package com.chutoutian.controller;

import com.chutoutian.common.Code;
import com.chutoutian.po.Product;
import com.chutoutian.service.ProductService;
import com.chutoutian.vo.ResponseMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/v1")
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping(value = "/product")
    public Object find(@RequestParam("categoryId") String categoryId) {
        return productService.findByCategory(categoryId);
    }

    @GetMapping(value = "/product/{id}")
    public Object get(@PathVariable("id") String id) {
        return productService.get(id);
    }

    @PostMapping(value = "/test")
    public Object test(@RequestParam("clientIp") String clientIp, @RequestParam("measurer") String measurer, @RequestParam("company") String company, @RequestParam("location") String location,  @RequestParam("collectTime") String collectTime, @RequestParam("dataContent") String dataContent) {
//    public Object test(@RequestParam("clientIp") String clientIp, @RequestParam("measurer") String measurer) {

        Map<String, String> map = new HashMap<>();
        map.put("clientIp", clientIp);
        map.put("measurer", measurer);
        map.put("company", company);
        map.put("location", location);
        map.put("collectTime", collectTime);
        map.put("dataContent", dataContent);
        return new ResponseMessage.Builder().code(Code.CODE_200).data(map).bulid();
    }

    @PostMapping(value = "/product")
    public Object add(@RequestBody Product product) {
        productService.add(product);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

    @PutMapping("/product")
    public Object update(@RequestBody Product product) {
        productService.update(product);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

    @DeleteMapping("/product/{id}")
    public Object delete(@PathVariable("id") String id) {
        productService.delete(id);
        return new ResponseMessage.Builder().code(Code.CODE_200).bulid();
    }

}
